package ystp.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import ystp.model.Ysrenyuanfenpei;
import ystp.service.FujianService;
import ystp.service.YsrenyuanfenpeiService;
import ystp.support.JsonResult;
import ystp.support.PageInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("HuizongbiaoView")
public class HuizongbiaoView {
    public Logger log = LoggerFactory.getLogger(FujianJieguoController.class);
    @Autowired
    public FujianService fujianService;
    @Autowired
    public YsrenyuanfenpeiService ddYsrenyuanfenpeiService;

    public boolean isLogin(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session == null || session.getAttribute("Id") == null) {
            return false;
        }
        return true;
    }
    @RequestMapping("/HuizongbiaoView")
    public String HuizongbiaoView(String xiangmuid, String xiangmuleixing, HttpServletRequest request, ModelMap map) {

        if (isLogin(request)) {
            map.put("xiangmuId", xiangmuid);
            map.put("xiangmuLeixing", xiangmuleixing);
            return "main/Fujian/Huizongbiaoview";
        } else {
            return "main/login";
        }

    }


    @RequestMapping("bindHuizongbiaoviewList")
    @ResponseBody
    public JsonResult<?> bindHuizongbiaoviewList(@RequestParam("xiangmuid") String ppxiangmuId, @RequestParam("pageindex") int ppPageIndex,
                                      @RequestParam("pagesize") int ppPageSize, @RequestParam("xiangmuleixing") String ppxiangmuleixing){
        Integer mmCount;
        List<Map<String,Object>> mmresultlist = new ArrayList<>();
        try {
            List<Map<String, Object>> list = fujianService.selectPingui(ppxiangmuleixing, ppxiangmuId, ppPageIndex, ppPageSize);
            mmCount = fujianService.countPingui(ppxiangmuleixing, ppxiangmuId);
            for (int i = 0; i < list.size(); i++) {
                int mmPinzhongHebingshu = 0;
                Map<String, Object> mmMap = list.get(i);
                if (mmMap != null) {
                    String mmPinzhongOne = mmMap.get("xuhao") == null ? "" : mmMap.get("xuhao").toString();
                    List<Map<String, Object>> mmPinguilist = fujianService.findShengchanqiyeByPinguiResult(ppxiangmuleixing, ppxiangmuId, mmPinzhongOne);
                    if (mmPinguilist.size() > 0) {
                        mmPinzhongHebingshu = mmPinguilist.size();
                    } else {
                        mmPinzhongHebingshu = mmPinzhongHebingshu + 1;
                    }
                    mmMap.put("pinguilist", mmPinguilist);
                    mmMap.put("pinzhonghebinghang", mmPinzhongHebingshu);
                }
                mmresultlist.add(mmMap);
            }
            //总人数
            List<Ysrenyuanfenpei> mmUserList=ddYsrenyuanfenpeiService.findUserByXiangmuId(ppxiangmuId);
            Double mm=((double)1/(double)2)*mmUserList.size();
            int mmTongguopiaoshu=(int)Math.ceil(mm);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("list", list);
            mmReturnMap.put("PageInfo", mmPageInfo);
            mmReturnMap.put("usersize", mmTongguopiaoshu);
            return JsonResult.getSuccessResult(mmReturnMap);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("HuizongbiaoView/bindHuizongbiaoviewList:error",e);
            return JsonResult.getErrorResult("HuizongbiaoView/bindHuizongbiaoviewList:error " + e.getMessage());
        }
    }
}
